/**
 * https://leetcode-cn.com/problems/max-area-of-island/
 * 695. 岛屿的最大面积
 * @param {number[][]} grid
 * @return {number}
 * 使用全局变量
 */
let area = 0
var maxAreaOfIsland = function (grid) {
    let max = -1
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            area = 0
            dfs(grid, i, j)
            max = Math.max(max, area)
        }
    }
    return max
};

const dfs = (grid, cr, cc) => {
    // 越界
    if (cr < 0 || cr >= grid.length || cc < 0 || cc >= grid[0].length) return
    // 已访问过, 或者不是岛屿
    if (grid[cr][cc] === -1 || grid[cr][cc] === 0) return

    // 面积+1, 并标记已经访问
    area++
    grid[cr][cc] = -1

    dfs(grid, cr - 1, cc) // 上
    dfs(grid, cr, cc + 1) // 右
    dfs(grid, cr + 1, cc) // 下
    dfs(grid, cr, cc - 1) // 左
}